package com.guigu.qingcheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.qingcheng.excption.GuliException;
import com.guigu.qingcheng.mapper.TbBrandMapper;
import com.guigu.qingcheng.mapper.TbCategoryMapper;
import com.guigu.qingcheng.mapper.TbSpecMapper;
import com.guigu.qingcheng.pojo.TbCategory;
import com.guigu.qingcheng.service.TbCategoryService;
import com.guigu.qingcheng.utils.CacheKey;
import com.guigu.qingcheng.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.*;


/**
*
* @author Jiang
* @date 2020/07/25
*/
@Service
public class TbCategoryServiceImpl implements TbCategoryService {
    @Autowired(required = false)
    private TbCategoryMapper tbCategoryMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired(required = false)
    private TbBrandMapper brandMapper;

    @Autowired(required = false)
    private TbSpecMapper tbSpecMapper;

    @Override
    public List<TbCategory> queryAll() {
        List<TbCategory> tbCategory = tbCategoryMapper.selectAll();
            return tbCategory;
    }

    @Override
    public PageUtil<TbCategory> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<TbCategory> pages = (Page<TbCategory>) tbCategoryMapper.selectAll();
        PageUtil<TbCategory> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<TbCategory> pageQueryByAll(Integer pageNum, Integer pageSize, TbCategory tbCategory) {

        Example example=new Example(TbCategory.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO 待实现 重新实现判断参数

        if(!StringUtils.isEmpty(tbCategory.getId())){
            criteria.andEqualTo("id",tbCategory.getId());
        }
        if(!StringUtils.isEmpty(tbCategory.getName())){
           criteria.andLike("name",tbCategory.getName());
        }
        if(!StringUtils.isEmpty(tbCategory.getParentId())){
            criteria.andEqualTo("parentId",tbCategory.getParentId());
        }
        PageHelper.startPage(pageNum,pageSize);
        Page<TbCategory> pages = (Page<TbCategory>) tbCategoryMapper.selectByExample(example);
        PageUtil<TbCategory> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
     }


    @Override
    public boolean saveTbCategory(TbCategory tbCategory) {
        int insert = tbCategoryMapper.insert(tbCategory);
        Map map = new HashMap();//保存分类名称 -brand和spec
        List list = new ArrayList();
        list.add(brandMapper.findByCategoryName(tbCategory.getName()));
        list.add(tbSpecMapper.findListByCategoryName(tbCategory.getName()));
        map.put(tbCategory.getName(),list);
        redisTemplate.opsForValue().set(tbCategory.getName(),map);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTbCategory(TbCategory tbCategory) {
        int i = tbCategoryMapper.updateByPrimaryKeySelective(tbCategory);
        if(i>0){
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteById(Integer id) {
        Example example=new Example(TbCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId",id);
        List<TbCategory> list = tbCategoryMapper.selectByExample(example);
        if(list.size()>0){
            throw new GuliException(20001,"存在下级菜单不能删除!");
        }
        redisTemplate.delete(list.get(0).getName());
        int i = tbCategoryMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public TbCategory findById(Integer id) {
        TbCategory tbCategory = tbCategoryMapper.selectByPrimaryKey(id);
        return tbCategory;
    }

    @Override
    public TbCategory findByIdName(Integer id) {
        TbCategory byIdName = tbCategoryMapper.findByIdName(id);
        return byIdName;
    }

    @Override
    public List<TbCategory> queryAllByparentId(Integer parentId) {
        Example example=new Example(TbCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId",parentId);
        List<TbCategory> tbCategories = tbCategoryMapper.selectByExample(example);
        return tbCategories;
    }

    @Override
    public List<Map> queryAllCategory() {
        List<TbCategory> tbCategories = tbCategoryMapper.selectAll();
        return findCategoryByParentId(0,tbCategories);
    }

    private List<Map> findCategoryByParentId(Integer parentId, List<TbCategory> categories){
        ArrayList<Map> maps = new ArrayList<>();
        for (TbCategory category:categories) {
            if (category.getParentId().equals(parentId)) {
                Map map = new HashMap();
                map.put("value", category.getId());
                map.put("label", category.getName());
                List<Map> children = findCategoryByParentId(category.getId(), categories);
                if (children.size()>0) {
                    map.put("children", children);
                }
                maps.add(map);
            }
        }
        return maps;
    }

    @Override
    public PageUtil<TbCategory> getCategoryByParentId(Integer parentId, Integer pageNo, Integer pageSize) {
        Example example = new Example(TbCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId", parentId);

        //分页插件
        PageHelper.startPage(pageNo, pageSize);
        List<TbCategory> categories = tbCategoryMapper.selectByExample(example);
        PageInfo<TbCategory> info = new PageInfo<>(categories);
        return new PageUtil<>(info.getTotal(), info.getList());
    }

    // 查询分类，树状结构
    @Override
    public List<Map> findCategoryTree() {
        // 查询商品分类导航
        Example example=new Example(TbCategory.class);
        Example.Criteria criteria = example.createCriteria();
        //判断is_show是否为1
        criteria.andEqualTo("isShow","1");
        //排序
        example.setOrderByClause("seq");
        List<TbCategory> tbCategories = tbCategoryMapper.selectByExample(example);
        return findByParentId(tbCategories,0);

    }

    @Override
    public List<Map> TodayPush() {
        List<Map> maps = tbCategoryMapper.TodayPush();
        return maps;
    }


    private List<Map> findByParentId(List<TbCategory> tbCategories, Integer parentId) {
        List<Map> mapList=new ArrayList<>();
        for (TbCategory category:tbCategories){
            if (category.getParentId().equals(parentId)){
                Map map = new HashMap();
                map.put("name",category.getName());

                map.put("menus",findByParentId(tbCategories,category.getId()));
                mapList.add(map);
            }
        }
       return mapList;
    }
}
